//
//  QuestionTableHeader.m
//  BMW
//
//  Created by up72-2 on 15/10/27.
//  Copyright © 2015年 up72-2. All rights reserved.
//

#import "QuestionTableHeader.h"
#import "AppDelegate.h"
#import "GdataParser.h"
static NSArray *tempArray;


@interface QuestionTableHeader()
{
    GdataParser *parser;
}
@end
@implementation QuestionTableHeader

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib
{
    popVC = [[ProductPopVC alloc] init];
    
    self.sex = 0;
    
    popOver = [[UIPopoverController alloc] initWithContentViewController:popVC];
    [popOver setPopoverContentSize:CGSizeMake(200.0,300.0)];
    
    self.boyLabel.font = [UIFont fontWithName:BMW_LIGHTTEXT size:15];
    self.girlLabel.font = [UIFont fontWithName:BMW_LIGHTTEXT size:15];
    
    //解析数据
    parser = [[GdataParser alloc]init];
    [parser start:^(NSMutableArray *arr) {
        tempArray = arr;
    }];
}

- (IBAction)provinceBtn:(UIButton *)sender
{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [popVC.myTableview setFrame:CGRectMake(0, 0, popOver.popoverContentSize.width, popOver.popoverContentSize.height)];
    
    switch (sender.tag) {
        case 1:
            {
                NSMutableArray *arr = [NSMutableArray array];
                for (int i=0; i<tempArray.count; i++) {
                    NSString *name = [[tempArray objectAtIndex:i] objectForKey:@"proName"];
                    [arr addObject:name];
                }
                [popVC resetTableData:arr];
                
                [popOver presentPopoverFromRect:sender.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            }
            break;
        case 2:
            {
                if (self.provinceBtn.titleLabel.text.length > 0) {
                    NSMutableArray *arr = [NSMutableArray array];
                    for (int i=0; i<tempArray.count; i++) {
                        NSString *name = [[tempArray objectAtIndex:i] objectForKey:@"proName"];;
                        if ([name isEqualToString:self.provinceBtn.titleLabel.text]) {
                            NSArray *arry = [[tempArray objectAtIndex:i] objectForKey:@"city"];
                            for (int j=0; j<arry.count; j++) {
                                NSString *name = [[arry objectAtIndex:j] objectForKey:@"cityName"];
                                [arr addObject:name];
                            }
                            break;
                        }
                    }
                    [popVC resetTableData:arr];
                    
                    
                    [popOver presentPopoverFromRect:sender.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
                }else
                {
                    [MTool showMessage:@"请选择省份" inView:app.window];
                }
            }
            break;
        case 100://男
            self.sex = 0;
            [self.nanImage setImage:[UIImage imageNamed:@"checked"]];
            [self.nvImage setImage:[UIImage imageNamed:@"unchecked"]];
            break;
        case 200://女
            self.sex = 1;
            [self.nanImage setImage:[UIImage imageNamed:@"unchecked"]];
            [self.nvImage setImage:[UIImage imageNamed:@"checked"]];
            break;
            
        default:
            break;
    }
    
    __weak typeof (self)weakSelf = self;
    __block UIPopoverController *tempPop = popOver;
    popVC.cellSelect = ^(NSString *str) {
        if (sender.tag==1) {
            [weakSelf.provinceBtn setTitle:str forState:0];
        }
        else
        {
            [weakSelf.cityBtn setTitle:str forState:0];
        }
        
        [tempPop dismissPopoverAnimated:YES];
    };
    
    
}
@end
